package nowcoder;

/**
 * 链表内指定区间反转
 * @author ：徐梦轲
 * @date ：2022/5/22 0022 17:46
 */
class ListNode {
    int val;
    ListNode next = null;

    public ListNode(int val) {
        this.val = val;
    }
}

public class Solution {
    public ListNode reverseBetween (ListNode head, int m, int n) {
        ListNode dummyNode = new ListNode(0);
        dummyNode.next = head;
        ListNode pre = dummyNode;
        for(int i = 0;i< m - 1;i++){
            pre = pre.next;
        }
        ListNode cur = pre.next;
        ListNode curNext = null;
        for (int i = 0; i < n - m; i++) {
            curNext = cur.next;
            cur.next = curNext.next;
            curNext.next = cur;
            pre.next = curNext;
        }
        return dummyNode.next;

    }
}
